﻿using System.Windows;
using System.Windows.Controls;

namespace Loki.UI.Wpf
{
    public class LokiTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;
            DataTemplate buffer = null;
            IScreen model = item as IScreen;

            if (element != null && model != null)
            {
                buffer = new DataTemplate();
                buffer.DataType = model.GetType();
                FrameworkElement template = UIContext.Templates.GetTemplate(model) as FrameworkElement;
                buffer.VisualTree = new FrameworkElementFactory(template.GetType());
            }

            if (buffer == null)
            {
                return base.SelectTemplate(item, container);
            }
            else
            {
                return buffer;
            }
        }
    }
}